﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;

namespace uam_fps_game.Network
{
    public static class ClientManager
    {
        private static NetClient client;
        static List<Player> players;
        public static string mes = "";
        public static bool IsAlive = false;

        public static void Connect(string host, int port)
        {
            NetPeerConfiguration confing = new NetPeerConfiguration("Configure");
            client = new NetClient(confing);
            players = new List<Player>();

          System.Threading.Thread.Sleep(4000); //to trzeba poprawic
            client.Start();
            System.Threading.Thread.Sleep(4000); //to trzeba poprawic
            NetOutgoingMessage hail = client.CreateMessage();
            hail.Write("Witamy na serwerze!");
            System.Threading.Thread.Sleep(4000); //to trzeba poprawic
            client.Connect(host, port, hail);
            System.Threading.Thread.Sleep(4000); //to trzeba poprawic
            IsAlive = true;
        }

        public static void ShutDown()
        {
            client.Disconnect("Bye bye");
            IsAlive = false;
        }

        public static void Send(string text)
        {
            NetOutgoingMessage om = client.CreateMessage(text);
            client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
        }

        public static void SendMessage(MessageTypes.Header header, MessageTypes.MessagePrototype message)
        {
            NetOutgoingMessage om = client.CreateMessage(); //to wrzucic na sztywno do srodka i zrobic liste MessagePrototype
            if ((int)header < 10)
                om.WriteVariableInt32(0);
            om.WriteVariableInt32((int)header);
            message.CreateMessage(ref om);
            client.SendMessage(om, NetDeliveryMethod.ReliableOrdered, 0);
        }

        public static void Update()
        {
            if (IsAlive)
            {
                mes = "";

                NetIncomingMessage msg;
                while ((msg = client.ReadMessage()) != null)
                {
                    ReceiveMessage(msg);
                    client.Recycle(msg);
                }
            }

        }

        public static void ReceiveMessage(NetIncomingMessage msg)
        {
            if (msg.MessageType == NetIncomingMessageType.Data)
            {
                //string str = msg.ReadString();
                int head = msg.ReadVariableInt32() * 10 + msg.ReadVariableInt32();

                switch (head)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        MessageTypes.Letter letter1 = MessageTypes.SendMessageToAll.ParseMessage(msg);
                        string mes1 = " " + letter1.senderName + ": " + letter1.text;
                        mes = mes1;
                        break;
                    case 4:
                        MessageTypes.Letter letter2 = MessageTypes.SendMessageToSmb.ParseMessage(msg);
                        string mes2 = " " + letter2.senderName + "says to you : " + letter2.text;
                        break;
                }

            }

        }


    }
}
